extends Node
@onready var player: CharacterBody2D = $"../../.."
@onready var displayer = $"../../../Display"
@onready var state_chart: StateChart = $"../StateChart"
@onready var tween_delay:int=0

func _ready() -> void:
	pass
#==================================================
##follow_mouse
func _on_follow_mouse_state_entered() -> void:
	displayer.pupil_follow_target=displayer.PupilFollowTarget.MOUSE
	pass # Replace with function body.


func _on_follow_mouse_state_exited() -> void:
	displayer.pupil_follow_target=displayer.PupilFollowTarget.NONE
	pass # Replace with function body.

#=======================================================
##surprise
var is_surprising:bool=false
func _on_surprise_state_entered() -> void:
	if is_surprising:return
	is_surprising=true
	var eye_color:=create_tween()
	var origin_color=displayer.eye_color
	eye_color.tween_property(displayer,"eye_color",Color(1.0, 0.784, 0.751, 1.0),0.2)
	eye_color.tween_property(self,"tween_delay",0,1.0)
	eye_color.tween_property(displayer,"eye_color",origin_color,0.2)
	var eye:=create_tween()
	eye.tween_property(displayer,"open_progress",2.0,0.2)
	eye.tween_property(self,"tween_delay",0,1.0)
	eye.tween_property(displayer,"open_progress",1.0,0.2)
	var twn:=create_tween()
	twn.tween_property(displayer,"pupil_radius",0.1,0.2)
	for i in 10:
		twn.tween_property(displayer,"look_at_position",Vector2(randf_range(-0.05,0.05),randf_range(-0.05,0.05)),0.1)
	twn.tween_property(displayer,"pupil_radius",0.2,0.2)
	twn.tween_callback(state_chart.send_event.bind("surprise_end"))
	print("surprise!!!")


func _on_surprise_state_exited() -> void:
	if not is_surprising:return
	is_surprising=false
	pass # Replace with function body.

#=========================================================
##blink
func _on_blink_state_entered() -> void:
	var open_pregress=displayer.open_progress
	var twn:Tween = create_tween()
	twn.tween_property(displayer,"open_progress",0.0,0.5)
	twn.tween_property(displayer,"open_progress",open_pregress,0.5)
	twn.tween_callback(state_chart.send_event.bind("eye_normal"))
	pass # Replace with function body.

func _on_blink_state_physics_processing(delta: float) -> void:
	pass # Replace with function body.

func _on_blink_state_exited() -> void:
	pass # Replace with function body.
#===============================================================
##look_around


func _on_look_around_state_entered() -> void:
	displayer.pupil_follow_target=displayer.PupilFollowTarget.NONE
	var twn:= create_tween()
	twn.tween_property(displayer,"look_at_position",Vector2(randf_range(-0.5,0.5),randf_range(-0.5,0.5)),0.2)
	twn.tween_property(self,"tween_delay",0,0.5)
	twn.tween_property(displayer,"look_at_position",Vector2(randf_range(-0.5,0.5),randf_range(-0.5,0.5)),0.2)
	twn.tween_property(self,"tween_delay",0,0.5)
	twn.tween_property(displayer,"look_at_position",Vector2(randf_range(-0.5,0.5),randf_range(-0.5,0.5)),0.2)
	twn.tween_property(self,"tween_delay",0,0.5)
	twn.tween_property(displayer,"look_at_position",Vector2(0.0,0.0),0.2)
	twn.tween_callback(state_chart.send_event.bind("blink"))
	twn.tween_property(self,"tween_delay",0,1.0)
	twn.tween_callback(func ():
		displayer.pupil_follow_target=displayer.PupilFollowTarget.MOUSE
		state_chart.send_event("look_around_end")
		)
	pass # Replace with function body.


func _on_look_around_state_exited() -> void:
	pass # Replace with function body.
#=================================================================
#pectinoma
@onready var pectinoma: Label = $"../../../Display/Pectinoma"
func _on_pectinoma_state_entered() -> void:
	var twn:=create_tween()
	twn.tween_property(displayer,"open_progress",0.3,0.1)
	twn.parallel().tween_property(pectinoma,"visible",true,0.1)
	twn.parallel().tween_property(pectinoma,"visible",true,1.0)
	twn.tween_callback(state_chart.send_event.bind("pectinoma_end"))
	
func _on_pectinoma_state_exited() -> void:
	var twn:=create_tween()
	twn.tween_property(displayer,"open_progress",1,0.1)
	twn.parallel().tween_property(pectinoma,"visible",false,0.1)

#==================================================================
#normal
func _on_normal_state_entered() -> void:
	if displayer.open_progress!=1.0:
		var twn:=create_tween()
		twn.tween_property(displayer,"open_progress",1.0,0.1)

#==========================================================================
#box scale

func _on_scale_state_physics_processing(delta: float) -> void:
	if $"../../..".now_move_mod!="suspend" and player.get_gravity()!=Vector2.ZERO:
		displayer.rotation=rotate_toward(displayer.rotation,0,delta*10)
		displayer.box_scale.y=move_toward(displayer.box_scale.y,clamp(1+player.velocity.y/3000,0.9,1.1),delta)
	else:
		if player.velocity!=Vector2.ZERO:
			displayer.rotation=rotate_toward(displayer.rotation,player.velocity.angle()-PI/2,delta*10)
		displayer.box_scale.y=move_toward(displayer.box_scale.y,1,delta)
